﻿<div class="ld-tabs">
    <ul>
        <li class="k-state-active">单据列表</li>
    </ul>
    <div>
        <div class="k-block divToolbar">
            <input type="text" placeholder="请输入单据号" id="txtKey" class="k-textbox"/>
            <div style="display: inline-block;">
                <input id="txtCustomer" data-value="0" style="width: 400px"/>
            </div>
            <div style="display: inline-block;">
                <select id="ddlMethod">
                    <option value="0">全部单据</option>
                    <option value="3">付款单</option>
                    <option value="4">收款单</option>
                </select>
            </div>
            <br />
            <br />
            <div style="display: inline-block;">
                <input id="txtOrderDateBefore" class="date" placeholder="开始时间" />
            </div>
            --
            <div style="display: inline-block;">
                <input id="txtOrderDateAfter" class="date" placeholder="结束时间" />
            </div>
            <button class="k-button" onclick="FilterOrders()"><span class="k-icon k-i-search"></span><span>搜索</span></button>
        </div>
        <div id="grid_orders" style="text-align: center"></div>
    </div>
</div>

<script class="init">
    LocatedMenu("财务管理", "单据查询");
    var allCustomer = Whale.Remote.GetEntity({
        url: "/Members/Customer/Gets",
        parameters: {
            customerStatus: 1
        }
    });
    $("#txtCustomer").kendoComboBox({
        placeholder: "请选择客户...",
        filter: "contains",
        dataTextField: "SearchLabel",
        template: "#=ComplexName#",
        dataValueField: "ID",
        dataSource: allCustomer,
        select: function (e) {
            var item = this.dataItem(e.item.index());
            $("#txtCustomer").attr("data-value", item.ID);
        },
        change: function (e) {
            var item = this.dataItem();
            if (item != undefined) {
                this.text(item.ComplexName);
            } else {
                var b = true;
                var obj = this;
                $.each(allCustomer, function (i, o) {
                    if (obj.text() == o.ComplexName) {
                        b = false;
                    }
                });
                if (b) {
                    this.text("");
                    $("#txtCustomer").attr("data-value", "0");
                }
            }
        }
    });

    $("#ddlMethod").kendoDropDownList();

    $(".date").kendoDatePicker({
        culture: "zh-CHS",
        format: "yyyy年MM月dd日",
    });
</script>

<script class="grid_customer_columns">
    var ordersFields = [{
        title: "单据号",
        field: "OrderNo",
        template: "#=GetOperations(OrderNo)#",
        width: 130
    }, {
        title: "客户",
        field: "Customer.ComplexName",
        width: 200
    }, {
        title: "总金额",
        field: "TotalMoney",
        format: "{0:c}",
        width: 100,
        attributes: { style: "text-align:right" }
    }, {
        title: "结余",
        field: "Balance",
        format: "{0:c}",
        width: 100,
        attributes: { style: "text-align:right" }
    }, {
        title: "日期",
        template: "#=kendo.toString(kendo.parseDate(NoteDate),'yyyy-MM-dd')#",
        width: 100
    }, {
        title: "相关交易单",
        field: "DealOrderList",
        template: "#=ReplaceComma(DealOrderList)#",
        attributes: { style: "text-align:left" },
        width: 130
    }, {
        title: "备注",
        field: "Description",
        width: 100
    }, {
        title: "经办人",
        field: "Operator",
        width: 100
    }, {}];

    function GetOperations(orderNo) {
        var html = "<a href=\"javascript:ShowBuyOrder('" + orderNo + "')\">" + orderNo + "</a>";
        return html;
    }

    function ShowBuyOrder(orderNo) {
        if (wndGeneralEdit != null && typeof wndGeneralEdit != "undefined") {
            wndGeneralEdit.setOptions({ title: "单据详情", width: 800 });
            wndGeneralEdit.refresh({ url: "/Orders/PaidOrder/Order/" + orderNo });
            wndGeneralEdit.open();
        }
    }

    function ReplaceComma(str) {
        var html = str.replace(/,/g, "<br />");
        return html;
    }
</script>

<script class="grid_config">
    $(function () {
        $("#grid_orders").kendoGrid({
            columns: ordersFields,
            dataSource: Whale.dataSource({
                url: "/Orders/PaidOrder/GetOrders",
                getParameters: function () {
                    return {
                        getCustomer: true,
                        customerID: $("#txtCustomer").attr("data-value"),
                        orderDateBefore: $("#txtOrderDateBefore").val(),
                        orderDateAfter: $("#txtOrderDateAfter").val(),
                        orderNo: $("#txtKey").val(),
                        method: $("#ddlMethod").data("kendoDropDownList").value(),
                        getOperator:true
                    };
                },
                pageSize: 20

            }),
            pageable: {
                messages: {
                    empty: "暂无数据"
                }
            },
            dataBound: function (e) {
                //让表头居中
                $("th.k-header").attr("style", "text-align:center; font-weight:bold;");
            }
        });
    });

    function FilterOrders() {
        $("#grid_orders").data("kendoGrid").dataSource.page(0);
        $("#grid_orders").data("kendoGrid").dataSource.read();
    }
</script>
